﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Public Class MediaInfo
    Shared Sub New()
        Dim aSelectColumns As String() = {"SharedItemId", "FeatureTitle", "VersionDbInfo", "VersionOmxInfo", "Genres", "OpenMediaInfoXml/OpenMediaInfo", "FeatureInfoStatus"}
        ObjectPersister.DefineObject(GetType(Entities.MediaInfo), "MediaInfo", aSelectColumns)
        ObjectPersister.AddCustomFieldHandler(GetType(Entities.MediaInfo), "Genres", AddressOf Handler_SetGenresField)
        ObjectPersister.AddCustomFieldHandler(GetType(Entities.MediaInfo), "OpenMediaInfo", AddressOf Handler_SetOpenMediaInfoField)
    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetBySharedItemId(ByVal sharedItemId As Integer) As Entities.MediaInfo
        Return ObjectPersister.GetOne(Of Entities.MediaInfo)(String.Format("SharedItemId={0}", sharedItemId))
    End Function

    Public Shared Function GetAllByShareId(ByVal shareId As Integer) As Generic.List(Of Entities.MediaInfo)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(String.Format("shareditemid in (select id from shareditems where Shareid = {0})", shareId))
    End Function
    Public Shared Function GetAllGenreCombinationsByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "SELECT DISTINCT Genres FROM MediaInfo WHERE Genres IS NOT NULL AND shareditemid in (select id from shareditems where ShareId = @ShareId)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndWithGenres(ByVal shareId As Integer, ByVal withGenres() As String) As Generic.List(Of Entities.MediaInfo)
        Dim strSql As String = "SharedItemId IN (SELECT Id from shareditems where ShareId = @ShareId)"
        If withGenres.Length > 0 Then
            For i As Integer = 0 To withGenres.Count - 1
                strSql = strSql & "AND Genres LIKE '%;" & withGenres(i) & ";%'"
            Next
        End If
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndInGenre(ByVal shareId As Integer, ByVal inGenre As Generic.List(Of String)) As Generic.List(Of Entities.MediaInfo)
        Dim strSql As String = "SharedItemId IN (SELECT Id from shareditems where ShareId = @ShareId) AND Genres LIKE @Genres"
        Dim strGenre As String = Framework.Serialization.ToDBSeparatorString(inGenre, ";"c) & "%"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Genres", strGenre)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strSql, sqlPar1, sqlPar2)
    End Function
    Public Shared Function GetAllByFeatureIds(ByVal featureIds As Generic.List(Of String)) As Generic.List(Of Entities.MediaInfo)
        Dim strSql As String = "SharedItemId in (select distinct shareditemid from mediainfo_featureids where featureid in ({0}))"
        Dim clsSB As New System.Text.StringBuilder
        Dim aParameters As New Generic.List(Of SqlCeParameter)
        For i As Integer = 0 To featureIds.Count - 1
            Dim strId As String = featureIds(i)
            If i > 0 Then clsSB.Append(",")
            clsSB.Append("@Id")
            clsSB.Append(i)
            aParameters.Add(New SqlCeParameter("@Id" & i.ToString, strId))
        Next
        strSql = String.Format(strSql, clsSB.ToString)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(aParameters.ToArray, strSql)
    End Function

    Public Shared Function GetAllCertificationCountriesByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "select distinct Country from mediainfo_certifications where shareditemid in (select id from shareditems where shareid=@ShareId) order by country"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetCertificationsLevelsByShareIdAndCountry(ByVal shareId As Integer, ByVal country As String) As Generic.List(Of String)
        Dim strSql As String = "select distinct Level from mediainfo_certifications where shareditemid in (select id from shareditems where shareid=@ShareId) and country=@Country order by level"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Country", country)
        Return Environment.SqlToStringList(strSql, sqlPar1, sqlPar2)
    End Function
    Public Shared Function GetAllByShareIdAndCertification(ByVal shareId As Integer, ByVal country As String, ByVal level As String) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_certifications where shareditemid in (select id from shareditems where shareid=@ShareId) and country=@Country and level=@Level)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Country", country)
        Dim sqlPar3 As New SqlCeParameter("@Level", level)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2, sqlPar3)
    End Function

    Public Shared Function GetAllActorsByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "select distinct name from mediainfo_cast where shareditemid in (select id from shareditems where shareid=@ShareId) order by name"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndActor(ByVal shareId As Integer, ByVal name As String) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_cast where shareditemid in (select id from shareditems where shareid=@ShareId) and name=@Name)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Name", name)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2)
    End Function

    Public Shared Function GetAllDirectorsByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "select distinct name from mediainfo_directors where shareditemid in (select id from shareditems where shareid=@ShareId) order by name"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndDirector(ByVal shareId As Integer, ByVal name As String) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_directors where shareditemid in (select id from shareditems where shareid=@ShareId) and name=@Name)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Name", name)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2)
    End Function

    Public Shared Function GetAllWritersByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "select distinct name from mediainfo_writers where shareditemid in (select id from shareditems where shareid=@ShareId) order by name"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndWriter(ByVal shareId As Integer, ByVal name As String) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_writers where shareditemid in (select id from shareditems where shareid=@ShareId) and name=@Name)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Name", name)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2)
    End Function

    Public Shared Sub DeleteBySharedItemId(ByVal sharedItemId As Integer, ByVal transactionScope As TransactionScope)
        Dim sqlPar1 As New SqlCeParameter("@SharedItemId", sharedItemId)
        Dim sqlCmd As Command = Environment.NewCommand(String.Empty, transactionScope)
        sqlCmd.AddParameterWithValue("@SharedItemid", sharedItemId)

        sqlCmd.CommandText = "DELETE FROM MediaInfo WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_Cast WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_Certifications WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_Directors WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_Writers WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)

        sqlCmd.CommandText = "DELETE FROM MediaInfo_AudioSpecs WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_VideoSpecs WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
        sqlCmd.CommandText = "DELETE FROM MediaInfo_FeatureIds WHERE SharedItemId=@SharedItemId"
        Environment.ExecuteNonQuery(sqlCmd)
    End Sub

    Private Shared Sub Handler_SetGenresField(ByVal obj As Object, ByVal value As Object)
        If value IsNot Nothing Then
            CType(obj, Entities.MediaInfo).Genres = Framework.Serialization.FromDBSeparatorString(CStr(value))
        End If
    End Sub
    Private Shared Sub Handler_SetOpenMediaInfoField(ByVal obj As Object, ByVal value As Object)
        If value IsNot Nothing Then
            CType(obj, Entities.MediaInfo).OpenMediaInfo = Framework.Serialization.FromXml(Of Entities.OpenMediaInfo.OpenMediaInfo)(CStr(value))
        End If
    End Sub

    Public Shared Sub Save(ByVal item As Entities.MediaInfo)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Save(item, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Save(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        ValidateItem(item)

        Dim clsCmd As New SaveCommand(Of Integer)("MediaInfo", "SharedItemId", item.SharedItemId)
        clsCmd.AddParam("FeatureTitle", item.FeatureTitle, True)
        clsCmd.AddParam("VersionDbInfo", item.VersionDbInfo, False)
        clsCmd.AddParam("VersionOmxInfo", item.VersionOmxInfo, False)
        clsCmd.AddParam("Genres", Framework.Serialization.ToDBSeparatorString(item.Genres, ";"c), True)
        If item.OpenMediaInfo IsNot Nothing Then
            clsCmd.AddParam("OpenMediaInfoXml", Framework.Serialization.ToXml(item.OpenMediaInfo), True)
        Else
            clsCmd.AddParam("OpenMediaInfoXml", DBNull.Value, True)
        End If
        clsCmd.AddParam("FeatureInfoStatus", item.FeatureInfoStatus, False)
        clsCmd.Save(transactionScope)

        'fill cast, directors and writers table
        UpdateCast(item, transactionScope)
        UpdateDirectors(item, transactionScope)
        UpdateWriters(item, transactionScope)
        'fill certifications table
        UpdateCertification(item, transactionScope)
        'fill featureids table
        UpdateFeatureIds(item, transactionScope)
        'fill audio specs table
        UpdateAudioSpecs(item, transactionScope)
        'fill Video specs table
        UpdateVideoSpecs(item, transactionScope)
    End Sub
    Private Shared Sub UpdateCast(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_Cast WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Feature IsNot Nothing Then
            If item.OpenMediaInfo.Feature.Cast IsNot Nothing Then
                strSql = "INSERT INTO MediaInfo_Cast (SharedItemId,Name) VALUES (@SharedItemId,@Name)"
                Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
                sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
                sqlCmd.AddParameterWithValue("@Name", String.Empty)
                For Each clsContributer As Entities.OpenMediaInfo.Contributer In item.OpenMediaInfo.Feature.Cast
                    sqlCmd.GetParameter("@Name").Value = clsContributer.Name
                    Environment.ExecuteNonQuery(sqlCmd)
                Next
            End If
        End If
    End Sub
    Private Shared Sub UpdateDirectors(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_Directors WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Feature IsNot Nothing Then
            If item.OpenMediaInfo.Feature.Cast IsNot Nothing Then
                strSql = "INSERT INTO MediaInfo_Directors (SharedItemId,Name) VALUES (@SharedItemId,@Name)"
                Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
                sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
                sqlCmd.AddParameterWithValue("@Name", String.Empty)
                For Each clsContributer As Entities.OpenMediaInfo.Contributer In item.OpenMediaInfo.Feature.Directors
                    sqlCmd.GetParameter("@Name").Value = clsContributer.Name
                    Environment.ExecuteNonQuery(sqlCmd)
                Next
            End If
        End If
    End Sub
    Private Shared Sub UpdateWriters(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_Writers WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Feature IsNot Nothing Then
            If item.OpenMediaInfo.Feature.Cast IsNot Nothing Then
                strSql = "INSERT INTO MediaInfo_Writers (SharedItemId,Name) VALUES (@SharedItemId,@Name)"
                Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
                sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
                sqlCmd.AddParameterWithValue("@Name", String.Empty)
                For Each clsContributer As Entities.OpenMediaInfo.Contributer In item.OpenMediaInfo.Feature.Writers
                    sqlCmd.GetParameter("@Name").Value = clsContributer.Name
                    Environment.ExecuteNonQuery(sqlCmd)
                Next
            End If
        End If
    End Sub

    Private Shared Function TruncateIfNeededForDb(ByVal text As String, ByVal maxLen As Integer) As Object
        If text Is Nothing Then
            Return DBNull.Value
        ElseIf text.Length <= maxLen Then
            Return text
        Else
            Return text.Substring(0, maxLen - 3) & "..."
        End If
    End Function

    Private Shared Sub UpdateCertification(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_Certifications WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Feature IsNot Nothing Then
            If item.OpenMediaInfo.Feature.Certifications IsNot Nothing Then
                strSql = "INSERT INTO MediaInfo_Certifications (SharedItemId,Country,Level,Note) VALUES (@SharedItemId,@Country,@Level,@Note)"
                Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
                sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
                sqlCmd.AddParameterWithValue("@Country", String.Empty)
                sqlCmd.AddParameterWithValue("@Level", String.Empty)
                sqlCmd.AddParameterWithValue("@Note", String.Empty)
                For Each clsCertification As Entities.OpenMediaInfo.Certification In item.OpenMediaInfo.Feature.Certifications
                    sqlCmd.GetParameter("@Country").Value = TruncateIfNeededForDb(clsCertification.Country, 32)
                    sqlCmd.GetParameter("@Level").Value = TruncateIfNeededForDb(clsCertification.Level, 32)
                    sqlCmd.GetParameter("@Note").Value = TruncateIfNeededForDb(clsCertification.Note, 32)
                    Environment.ExecuteNonQuery(sqlCmd)
                Next
            End If
        End If
    End Sub

    Private Shared Sub UpdateFeatureIds(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_FeatureIds WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Feature IsNot Nothing Then
            If item.OpenMediaInfo.Feature.Ids IsNot Nothing Then
                strSql = "INSERT INTO MediaInfo_FeatureIds (SharedItemId,FeatureId) VALUES (@SharedItemId,@FeatureId)"
                Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
                sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
                sqlCmd.AddParameterWithValue("@FeatureId", String.Empty)
                For Each strId As String In item.OpenMediaInfo.Feature.Ids
                    sqlCmd.GetParameter("@FeatureId").Value = TruncateIfNeededForDb(strId, 32)
                    Environment.ExecuteNonQuery(sqlCmd)
                Next
            End If
        End If
    End Sub

    Private Shared Sub UpdateAudioSpecs(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_AudioSpecs WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Streams IsNot Nothing Then
            strSql = "INSERT INTO MediaInfo_AudioSpecs (SharedItemId,Channels,ChannelPositions,Language,Format,Bitrate) VALUES (@SharedItemId,@Channels,@ChannelPositions,@Language,@Format,@Bitrate)"
            Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
            sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
            sqlCmd.AddParameterWithValue("@Channels", String.Empty)
            sqlCmd.AddParameterWithValue("@ChannelPositions", String.Empty)
            sqlCmd.AddParameterWithValue("@Language", String.Empty)
            sqlCmd.AddParameterWithValue("@Format", String.Empty)
            sqlCmd.AddParameterWithValue("@Bitrate", String.Empty)
            For Each clsS As Entities.OpenMediaInfo.StreamInfo In item.OpenMediaInfo.Streams
                If clsS.AudioStreams IsNot Nothing Then
                    For Each clsAS As Entities.OpenMediaInfo.AudioStreamInfo In clsS.AudioStreams
                        sqlCmd.GetParameter("@Channels").Value = TruncateIfNeededForDb(clsAS.Channels, 16)
                        sqlCmd.GetParameter("@ChannelPositions").Value = TruncateIfNeededForDb(clsAS.ChannelPositions, 16)
                        sqlCmd.GetParameter("@Language").Value = TruncateIfNeededForDb(clsAS.Language, 16)
                        sqlCmd.GetParameter("@Format").Value = TruncateIfNeededForDb(clsAS.Format, 16)
                        sqlCmd.GetParameter("@Bitrate").Value = TruncateIfNeededForDb(clsAS.Bitrate, 16)
                        Environment.ExecuteNonQuery(sqlCmd)
                    Next
                End If
            Next
        End If
    End Sub

    Private Shared Sub UpdateVideoSpecs(ByVal item As Entities.MediaInfo, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM MediaInfo_VideoSpecs WHERE SharedItemId={0}", item.SharedItemId)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        If item.OpenMediaInfo.Streams IsNot Nothing Then
            strSql = "INSERT INTO MediaInfo_VideoSpecs (SharedItemId,Width,Height,AspectRatio,Bitrate,Format,Framerate) VALUES (@SharedItemId,@Width,@Height,@AspectRatio,@Bitrate,@Format,@Framerate)"
            Dim sqlCmd As Command = Environment.NewCommand(strSql, transactionScope)
            sqlCmd.AddParameterWithValue("@SharedItemId", item.SharedItemId)
            sqlCmd.AddParameterWithValue("@Width", CInt(0))
            sqlCmd.AddParameterWithValue("@Height", CInt(0))
            sqlCmd.AddParameterWithValue("@AspectRatio", String.Empty)
            sqlCmd.AddParameterWithValue("@Bitrate", String.Empty)
            sqlCmd.AddParameterWithValue("@Format", String.Empty)
            sqlCmd.AddParameterWithValue("@Framerate", String.Empty)
            For Each clsS As Entities.OpenMediaInfo.StreamInfo In item.OpenMediaInfo.Streams
                If clsS.VideoStreams IsNot Nothing Then
                    For Each clsVS As Entities.OpenMediaInfo.VideoStreamInfo In clsS.VideoStreams
                        sqlCmd.GetParameter("@Width").Value = clsVS.Width
                        sqlCmd.GetParameter("@Height").Value = clsVS.Height
                        sqlCmd.GetParameter("@AspectRatio").Value = TruncateIfNeededForDb(clsVS.AspectRatio, 16)
                        sqlCmd.GetParameter("@Bitrate").Value = TruncateIfNeededForDb(clsVS.Bitrate, 16)
                        sqlCmd.GetParameter("@Format").Value = TruncateIfNeededForDb(clsVS.Format, 16)
                        sqlCmd.GetParameter("@Framerate").Value = TruncateIfNeededForDb(clsVS.Framerate, 16)
                        Environment.ExecuteNonQuery(sqlCmd)
                    Next
                End If
            Next
        End If
    End Sub

    Public Shared Function GetAllMoviePictureSizesByShareId(ByVal shareId As Integer) As Generic.List(Of Entities.Size)
        Dim strSql As String = "select distinct Width,Height from mediainfo_videospecs where shareditemid in (select id from shareditems where shareid=@ShareId) order by width,height"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToInfoObjects(Of Entities.Size)(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndSize(ByVal shareId As Integer, ByVal minWidth As Short, ByVal maxWidth As Short) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_videospecs where shareditemid in (select id from shareditems where shareid=@ShareId) and Width>=@Min and Width<=@Max)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Min", minWidth)
        Dim sqlPar3 As New SqlCeParameter("@Max", maxWidth)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2, sqlPar3)
    End Function

    Public Shared Function GetAllAudioLanguagesByShareId(ByVal shareId As Integer) As Generic.List(Of String)
        Dim strSql As String = "select distinct language from mediainfo_audiospecs where shareditemid in (select id from shareditems where shareid=@ShareId) order by language"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Return Environment.SqlToStringList(strSql, sqlPar1)
    End Function
    Public Shared Function GetAllByShareIdAndAudioLanguage(ByVal shareId As Integer, ByVal language As String) As Generic.List(Of Entities.MediaInfo)
        Dim strClause As String = "shareditemid in (select shareditemid from mediainfo_audiospecs where shareditemid in (select id from shareditems where shareid=@ShareId) and language=@Language)"
        Dim sqlPar1 As New SqlCeParameter("@ShareId", shareId)
        Dim sqlPar2 As New SqlCeParameter("@Language", language)
        Return ObjectPersister.GetAll(Of Entities.MediaInfo)(strClause, sqlPar1, sqlPar2)
    End Function



    Private Shared Sub ValidateItem(ByVal item As Entities.MediaInfo)
        If item.SharedItemId < 1 Then Throw New ArgumentException("SharedItemId not valid!")
    End Sub

End Class
